package com.tianya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.entity.Course;
import com.tianya.domain.entity.Schedule;
import com.tianya.domain.entity.Teacher;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.ScheduleVO;
import com.tianya.mapper.TeacherMapper;
import com.tianya.service.CourseService;
import com.tianya.service.ScheduleService;
import com.tianya.mapper.ScheduleMapper;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【schedule(排课表)】的数据库操作Service实现
* @createDate 2025-05-24 22:22:15
*/
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule>
    implements ScheduleService{
    @Resource
    private TeacherMapper  teacherMapper;

    @Resource
    private CourseService courseService;

    @Resource
    private UserService userService;
    @Override
    public List<Schedule> getScheduleByStudentId(Long studentId) {
        return list(new QueryWrapper<Schedule>()
                .eq("student_id", studentId));
    }

    @Override
    public List<Schedule> listByTeacherId(Long teacherId) {
        return list(new QueryWrapper<Schedule>()
                .eq("teacher_id", teacherId));
    }

    @Override
    public List<ScheduleVO> getScheduleByClazzId(Long clazzId) {
        List<Schedule> list = list(new QueryWrapper<Schedule>().eq("clazz_id", clazzId));
        List<ScheduleVO> scheduleVOS = new ArrayList<>();
        for (Schedule schedule : list) {
            Long teacherId = schedule.getTeacherId();
            Teacher teacher = teacherMapper.selectById(teacherId);
            Long userId = teacher.getUserId();
            User user = userService.getById(userId);
            Course course = courseService.getById(schedule.getCourseId());

            ScheduleVO scheduleVO = ScheduleVO.builder()
                    .id(schedule.getScheduleId())
                    .courseId(schedule.getCourseId())
                    .courseName(course.getName())
                    .teacherId(teacherId)
                    .teacherName(user!=null?user.getUsername():"unknown")
                    .location(schedule.getLocation())
                    .dayOfWeek(schedule.getDayOfWeek())
                    .period(schedule.getPeriod())
                    .startTime(schedule.getStartTime())
                    .endTime(schedule.getEndTime())
                    .location(schedule.getLocation())
                    .build();
            scheduleVOS.add(scheduleVO);
        }
        return scheduleVOS;
    }
}




